package com.ruoyi.system.listener;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.bo.PProblemTopicBo;
import com.ruoyi.system.service.IPProblemTopicService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 心理题库自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class TopicImportListener extends AnalysisEventListener<PProblemTopicBo> implements ExcelListener<PProblemTopicBo> {

    private final IPProblemTopicService topicService;
    private final Boolean isUpdateSupport;
    private final Long topicId;
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public TopicImportListener(Boolean isUpdateSupport, Long topicId) {
        this.topicService = SpringUtils.getBean(IPProblemTopicService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.topicId = topicId;
    }

    @Override
    public void invoke(PProblemTopicBo bo, AnalysisContext context) {
        try {
            // 验证
            if (ObjectUtil.isNotEmpty(bo)) {
                if (ObjectUtil.isNotNull(topicId)) {
                    bo.setTopicId(this.topicId);
                }
                topicService.insertByBo(bo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、").append(" 导入成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、").append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、" + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<PProblemTopicBo> getExcelResult() {
        return new ExcelResult<PProblemTopicBo>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<PProblemTopicBo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
